package cn.chinasyq.photoquan.photo.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import cn.chinasyq.photoquan.PreviewNetworkPhotoActivity;
import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.photo.bean.CommentsEntity;
import cn.master.volley.commons.VolleyHelper;

/**
 * Created by 一搏 on 2015/11/25.
 */
public class PhotoCommentAdapter extends BaseAdapter {
    private List<CommentsEntity> infoList;
    private ArrayList<Object> urlList;

    public PhotoCommentAdapter() {
        infoList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return infoList.size();
    }

    @Override
    public Object getItem(int position) {
        return infoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object item = getItem(position);
        return getCommentsEntity((CommentsEntity) item, convertView, parent);
    }




    class CommentViewHolder {
        TextView tv_name, tv_time, tv_content;
        CheckedTextView tv_like;
        NetworkImageView iv_avatar,imageView2;

    }

    private View getCommentsEntity(CommentsEntity contententity, View convertView, ViewGroup parent) {
        CommentViewHolder titleViewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof CommentViewHolder)) {
            titleViewHolder = new CommentViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_info_comment, null);
            titleViewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            titleViewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            titleViewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            titleViewHolder.tv_like = (CheckedTextView) convertView.findViewById(R.id.tv_like_num);
            titleViewHolder.tv_like.setOnClickListener((View.OnClickListener) parent.getContext());
            titleViewHolder.iv_avatar = (NetworkImageView) convertView.findViewById(R.id.iv_avatar);
            titleViewHolder.imageView2 = (NetworkImageView) convertView.findViewById(R.id.iv_1);
            titleViewHolder.imageView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), PreviewNetworkPhotoActivity.class);
                    urlList = new ArrayList<>();
                    urlList.add(v.getTag().toString());
                    intent.putExtra(PreviewNetworkPhotoActivity.INTENT_FILE_PATH,
                            urlList);
                    intent.putExtra(PreviewNetworkPhotoActivity.INTENT_POSITION,
                            0);
                    intent.putExtra(PreviewNetworkPhotoActivity.MODE,PreviewNetworkPhotoActivity.MODE_PREVIEW);
                    v.getContext().startActivity(intent);
                }
            });
            convertView.setTag(titleViewHolder);
        } else {
            titleViewHolder = (CommentViewHolder) convertView.getTag();
        }
        titleViewHolder.tv_time.setText(contententity.getComment().getAgo());
        titleViewHolder.tv_name.setText(contententity.getUser().getName());
        titleViewHolder.tv_content.setText(contententity.getComment().getContent());
        titleViewHolder.tv_like.setText(String.valueOf(contententity.getComment().getLike_count()));
        titleViewHolder.tv_like.setChecked(contententity.getComment().isIs_like());
        titleViewHolder.tv_like.setTag(contententity.getComment().getUuid());
        titleViewHolder.tv_like.setEnabled(!contententity.getComment().isIs_like());
        titleViewHolder.iv_avatar.setImageUrl(contententity.getUser().getAvatar(), VolleyHelper.getImageLoader());
        titleViewHolder.imageView2.setImageUrl(contententity.getComment().getImage(), VolleyHelper.getImageLoader());
        titleViewHolder.imageView2.setTag(contententity.getComment().getImage());
        if (StringUtils.isEmpty(contententity.getComment().getImage())){
            titleViewHolder.imageView2.setVisibility(View.GONE);
        }else {
            titleViewHolder.imageView2.setVisibility(View.VISIBLE);
        }
        return convertView;
    }


    public void setInfoEntity(List<CommentsEntity> infoEntity) {
        infoList=infoEntity;
        notifyDataSetChanged();
    }
    public void addInfoEntity(List<CommentsEntity> infoEntity) {
        infoList.addAll(infoEntity);
        notifyDataSetChanged();
    }


}
